// pages/usercenter/userinfo/userinfo.js
const http = require("../../../utils/http");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loginInfo: {},
        avatarUrl: "",
        sexList: [{value: 1, text: "男"}, {value: 2, text: "女"}, {value: 3, text: "未选择"}],
        showSex: false,
        sexIndex: 0,
        showBirthday: false,
        birthday: "",
        minDate: new Date(1960, 0, 1).getTime(),
        maxDate: new Date().getTime()
    },
    getLoginInfo() {
        http.get('wx/vip/info', {}, false).then(data => {
            let sexIndex = 0;
            for (let i = 0; i < this.data.sexList.length; i++) {
                if (this.data.sexList[i].value === data.sex) {
                    sexIndex = i;
                    break;
                }
            }
            let birthday = new Date().getTime();
            if (data.birthday) {
                const bs = data.birthday.split("-")
                birthday = new Date(data.birthday).getTime();
            }

            this.setData({
                loginInfo: {...data},
                avatarUrl: http.baseUrl + data.avatar,
                sexIndex,
                birthday
            })
        }).catch(() => {
            wx.showToast({title: "请先登录！", icon: "error"})
            wx.reLaunch({url: "/pages/index/index"})
        })
    },
    getAvatar(e) {
        const avatarUrl = e.detail.avatarUrl
        this.setData({
            avatarUrl
        })
        wx.uploadFile({
            url: http.baseUrl + "wx/upload",
            filePath: avatarUrl,
            name: "file",
            header: {wxToken: wx.getStorageSync("wxToken")},
            success: res => {
                const data = JSON.parse(res.data)
                this.setData({
                    "loginInfo.avatar": data.data
                })
            },
            error: e => {
                console.log(e)
            }
        })
    },
    submit(e) {
        const vipInfo = {
            vipId: this.data.loginInfo.vipId,
            name: this.data.loginInfo.name,
            avatar: this.data.loginInfo.avatar,
            sex: this.data.loginInfo.sex,
            birthday: this.data.loginInfo.birthday,
            profile: e.detail.value.profile,
        }
        http.post('wx/vip/save', vipInfo).then(() => {
            wx.showToast({title: "更新成功", icon: "success"})
            this.getLoginInfo()
        })
    },
    getNickname(e) {
        this.setData({
            "loginInfo.name": e.detail.value
        })
    },
    showSexPop() {
        this.setData({
            showSex: true
        })
    },
    closeSexPop() {
        this.setData({
            showSex: false
        })
    },
    changeSex(e) {
        this.setData({
            sexIndex: e.detail.index,
            "loginInfo.sex": e.detail.value.value
        })
    },
    showBirthdayPop() {
        this.setData({
            showBirthday: true
        })
    },
    getBirthday(e) {
        const time = e.detail;
        const date = new Date(time);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const birthday = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`
        this.setData({
            birthday: time,
            "loginInfo.birthday": birthday,
            showBirthday: false
        })
    },
    closeBirthday() {
        this.setData({
            showBirthday: false
        })
    },
    phone() {
        wx.navigateTo({url: "/pages/usercenter/phone/phone"})
    },
    getOpenId() {
        wx.login({
            success: res => {
                http.get('wx/vip/openid/save', {jsCode: res.code}).then(() => {
                    wx.showToast({title: "绑定成功！", icon: "success"})
                    this.getLoginInfo()
                })
            }
        })
    },
    delVip() {
        wx.showModal({
            title: "提示",
            content: "确定注销账号吗？",
            showCancel: true,
            success: res => {
                if (res.confirm) {
                    wx.showModal({
                        title: "提示",
                        content: "注销后所有数据不可找回！确定注销吗？",
                        showCancel: true,
                        success: res => {
                            if (res.confirm) {
                                http.delete('wx/vip/delete').then(() => {
                                    wx.showToast({title: "再见了~！"})
                                    wx.removeStorageSync("wxToken")
                                    setTimeout(() => {
                                        wx.reLaunch({url: "/pages/index/index"})
                                    }, 1500)
                                })
                            }
                        }
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getLoginInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
